from flask_restful import Resource

from model.crud import QueryMultipleInstances
from model.tables import Blog, Comment, Like, Follow
from restful.data import DataMyPosts
from restful.response import ResponseSuccess
from restful.session import current_user_required,data

def _MyPosts(posts):
    return [{
        'pid': i.id,
        'title': i.title,
        'time': i.time,
        'sort': i.sort,
        **({'reason': i.reason} if i.checked and not i.passed else {})
    } for i in posts]

class MyPosts(Resource):
    @current_user_required
    @data(DataMyPosts())
    def get(self,current_user,data):
        posts = QueryMultipleInstances(Blog,{'uid':current_user.id,'checked':data['checked'],'passed':data['passed']})
        return ResponseSuccess(data={
            "blogs": _MyPosts(posts)
        })

class MyComments(Resource):
    @current_user_required
    def get(self,current_user):
        comments = QueryMultipleInstances(Comment,{'uid':current_user.id})
        return ResponseSuccess(data={
            "comments":[{
                "pid":i.pid,
                "cid":i.id,
                "time":i.time,
                "content":i.content,
            }for i in comments]
        })

class MyLikes(Resource):
    @current_user_required
    def get(self,current_user):
        likes = QueryMultipleInstances(Like,{'uid':current_user.id})
        return ResponseSuccess(data={
            'likes':[{
                'pid':i.pid,
                'time':i.time,
            } for i in likes]
        })

class MyFollows(Resource):
    @current_user_required
    def get(self,current_user):
        follows = QueryMultipleInstances(Follow,{'uid':current_user.id})
        return ResponseSuccess(data={
            'follows':[{
                'uid':i.uid1,
                'time':i.time,
            }for i in follows]
        })